//
//  ChangeNicknameViewController.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/3/5.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "ChangeNicknameViewController.h"
#import "MyInforViewController.h"
@interface ChangeNicknameViewController ()

@end

@implementation ChangeNicknameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.isInviter) {
        [self setNaviTitle:@"我的邀请人"];
        self.nickTF.placeholder=@"输入邀请码";
        self.backupLabel.hidden=YES;
        self.nickTF.keyboardType=UIKeyboardTypeNumberPad;
    }else{
        [self setNaviTitle:@"昵称"];
    }
   
    [self setNaviLeftButton:@""];
    [self setNaviRightButton:@"保存"];
    self.bgView.layer.cornerRadius=5;
    self.bgView.layer.borderWidth=0.5;
    self.bgView.layer.borderColor=[UIColor colorWithHexString:@"d8d8d8"].CGColor;
    [self.nickTF becomeFirstResponder];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)saveData{
    if (self.isInviter) {
        //保存邀请码
        [[LKDHud shareHud] showLoadingView];
        [ApiClient saveInvitationCode:self.nickTF.text DidFinishLoaded:^(KXJson *json, NSString *path) {
            [[LKDHud shareHud] hideLoadingView];
            if ([[json getStringForKey:@"status"] intValue]==1) {
                [self.navigationController popViewControllerAnimated:YES];
            }
        } didFailLoaded:^(NSError *error, NSString *path) {
             [[LKDHud shareHud] hideLoadingView];
        }];
    }else{
        if (self.nickTF.text.length<4) {
            [[LKDHud shareHud] showtip:@"不能小于4位"];
            return;
        }
        [[LKDHud shareHud] showLoadingView];
        [ApiClient changeFirstName:self.nickTF.text didFinishLoaded:^(KXJson *json, NSString *path) {
            [[LKDHud shareHud] hideLoadingView];
            if ([[json getStringForKey:@"status"] intValue]==1) {
                MyInforViewController *vc=(MyInforViewController*)self.vcDelegate;
                [vc reloadNickName:self.nickTF.text];
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        } didFailLoaded:^(NSError *error, NSString *path) {
            [[LKDHud shareHud] hideLoadingView];
            
        }];
 
    }
   }

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (toBeString.length > 20) {
        
        textField.text = [toBeString substringToIndex:20];
        
        return NO;
        
    }
    
    return YES;
    
}
-(void)clickRightButton:(UIButton*)bt{
    [self saveData];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
